<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JavaScript中各种源码实现（前端面试笔试必备） | 时间领主的博客</title>
    <meta name="generator" content="VuePress 1.4.1">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    <link rel="icon" href="/blog/favicon.ico">
    <meta name="description" content="行动不一定带来快乐，而无行动则决无快乐">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/blog/assets/css/0.styles.174d23e9.css" as="style"><link rel="preload" href="/blog/assets/js/app.c6f0b635.js" as="script"><link rel="preload" href="/blog/assets/js/3.56d0db2f.js" as="script"><link rel="preload" href="/blog/assets/js/1.dc177a2a.js" as="script"><link rel="preload" href="/blog/assets/js/21.5e84bd81.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.3ea1e65e.js"><link rel="prefetch" href="/blog/assets/js/11.b138a709.js"><link rel="prefetch" href="/blog/assets/js/12.98d3dec7.js"><link rel="prefetch" href="/blog/assets/js/13.1a8500fb.js"><link rel="prefetch" href="/blog/assets/js/14.d52be3d5.js"><link rel="prefetch" href="/blog/assets/js/15.e6ebcc21.js"><link rel="prefetch" href="/blog/assets/js/16.515c742f.js"><link rel="prefetch" href="/blog/assets/js/17.8ebf8e60.js"><link rel="prefetch" href="/blog/assets/js/18.b7c7c6f0.js"><link rel="prefetch" href="/blog/assets/js/19.0ccdcb93.js"><link rel="prefetch" href="/blog/assets/js/20.78e9f5a5.js"><link rel="prefetch" href="/blog/assets/js/22.eff4bf70.js"><link rel="prefetch" href="/blog/assets/js/23.1417d381.js"><link rel="prefetch" href="/blog/assets/js/24.013e4313.js"><link rel="prefetch" href="/blog/assets/js/25.f3067eb6.js"><link rel="prefetch" href="/blog/assets/js/26.19fe9c6d.js"><link rel="prefetch" href="/blog/assets/js/27.c089c5d4.js"><link rel="prefetch" href="/blog/assets/js/28.6ff3368c.js"><link rel="prefetch" href="/blog/assets/js/29.d1cc7d1f.js"><link rel="prefetch" href="/blog/assets/js/30.0dc56898.js"><link rel="prefetch" href="/blog/assets/js/31.b5bac1d5.js"><link rel="prefetch" href="/blog/assets/js/32.fbc30039.js"><link rel="prefetch" href="/blog/assets/js/33.7070e81d.js"><link rel="prefetch" href="/blog/assets/js/34.e7c86927.js"><link rel="prefetch" href="/blog/assets/js/35.c731e557.js"><link rel="prefetch" href="/blog/assets/js/36.151c6972.js"><link rel="prefetch" href="/blog/assets/js/37.019d3b29.js"><link rel="prefetch" href="/blog/assets/js/4.6a4255d5.js"><link rel="prefetch" href="/blog/assets/js/5.829faed3.js"><link rel="prefetch" href="/blog/assets/js/6.b91d75cb.js"><link rel="prefetch" href="/blog/assets/js/7.7aafbc48.js"><link rel="prefetch" href="/blog/assets/js/8.982bf0b3.js"><link rel="prefetch" href="/blog/assets/js/9.283b0ce1.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.174d23e9.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-4698c43e><div data-v-4698c43e><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-4698c43e data-v-4698c43e><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-6cbeab0a data-v-4698c43e data-v-4698c43e><h3 class="title" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a>时间领主的博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><input type="password" value="" data-v-6cbeab0a> <span data-v-6cbeab0a>Konck! Knock!</span> <button data-v-6cbeab0a>OK</button></label> <div class="footer" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><span data-v-6cbeab0a><i class="iconfont reco-theme" data-v-6cbeab0a></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-6cbeab0a>vuePress-theme-reco</a></span> <span data-v-6cbeab0a><i class="iconfont reco-copyright" data-v-6cbeab0a></i> <a data-v-6cbeab0a><span data-v-6cbeab0a>时间领主</span>
            
          <span data-v-6cbeab0a>2019 - </span>
          2020
        </a></span></div></div> <div class="hide" data-v-4698c43e><header class="navbar" data-v-4698c43e><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">时间领主的博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Light-UI 组件库/" class="nav-link"><i class="iconfont undefined"></i>
  Light-UI 组件库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据库/" class="nav-link"><i class="iconfont undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/问题/" class="nav-link"><i class="iconfont undefined"></i>
  问题
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="https://gitee.com/caohuiboss" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><a href="https://cn.bing.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  必应
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-4698c43e></div> <aside class="sidebar" data-v-4698c43e><div class="personal-info-wrapper" data-v-6c8ffc9c><img src="/blog/avatar.gif" alt="author-avatar" class="personal-img" data-v-6c8ffc9c> <h3 class="name" data-v-6c8ffc9c>
    时间领主
  </h3> <div class="num" data-v-6c8ffc9c><div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>26</h3> <h6 data-v-6c8ffc9c>文章</h6></div> <div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>8</h3> <h6 data-v-6c8ffc9c>标签</h6></div></div> <hr data-v-6c8ffc9c></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Light-UI 组件库/" class="nav-link"><i class="iconfont undefined"></i>
  Light-UI 组件库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据库/" class="nav-link"><i class="iconfont undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/问题/" class="nav-link"><i class="iconfont undefined"></i>
  问题
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="https://gitee.com/caohuiboss" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><a href="https://cn.bing.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  必应
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>JavaScript中各种源码实现（前端面试笔试必备）</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个new操作符" class="sidebar-link">实现一个new操作符</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个array-isarray" class="sidebar-link">实现一个Array.isArray</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个object-create-方法" class="sidebar-link">实现一个Object.create()方法</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个eventhub-发布订阅" class="sidebar-link">实现一个EventHub 发布订阅</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个array-prototype-reduce" class="sidebar-link">实现一个Array.prototype.reduce</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个call或apply" class="sidebar-link">实现一个call或apply</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#call" class="sidebar-link">call</a></li><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一下apply，唯一不同的是参数的处理-代码如下：" class="sidebar-link">实现一下apply，唯一不同的是参数的处理,代码如下：</a></li></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个function-prototype-bind" class="sidebar-link">实现一个Function.prototype.bind</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个js函数柯里化" class="sidebar-link">实现一个JS函数柯里化</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#手写防抖-debouncing-和节流-throttling" class="sidebar-link">手写防抖(Debouncing)和节流(Throttling)</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#节流" class="sidebar-link">节流</a></li><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#节流-2" class="sidebar-link">节流</a></li></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#手写一个js深拷贝" class="sidebar-link">手写一个JS深拷贝</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#丐版" class="sidebar-link">丐版</a></li><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#基础版" class="sidebar-link">基础版</a></li><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#旗舰版" class="sidebar-link">旗舰版</a></li></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个instanceof" class="sidebar-link">实现一个instanceOf</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现原型链继承" class="sidebar-link">实现原型链继承</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现一个事件代理" class="sidebar-link">实现一个事件代理</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#实现vue双向绑定" class="sidebar-link">实现vue双向绑定</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#vue-2-x的object-defineproperty版本" class="sidebar-link">Vue 2.x的Object.defineProperty版本</a></li><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/%E9%9D%A2%E8%AF%95%E9%A2%98/javascript%E5%B8%B8%E8%A7%81%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0.html#vue-3-x的proxy-版本" class="sidebar-link">Vue 3.x的proxy 版本</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-6cbeab0a data-v-4698c43e><h3 class="title" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a>JavaScript中各种源码实现（前端面试笔试必备）</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><input type="password" value="" data-v-6cbeab0a> <span data-v-6cbeab0a>Konck! Knock!</span> <button data-v-6cbeab0a>OK</button></label> <div class="footer" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><span data-v-6cbeab0a><i class="iconfont reco-theme" data-v-6cbeab0a></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-6cbeab0a>vuePress-theme-reco</a></span> <span data-v-6cbeab0a><i class="iconfont reco-copyright" data-v-6cbeab0a></i> <a data-v-6cbeab0a><span data-v-6cbeab0a>时间领主</span>
            
          <span data-v-6cbeab0a>2019 - </span>
          2020
        </a></span></div></div> <div data-v-4698c43e><main class="page"><!----> <div class="page-title" style="display:none;"><h1>JavaScript中各种源码实现（前端面试笔试必备）</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>时间领主</span></i> <i class="iconfont reco-date" data-v-484a899e><span data-v-484a899e>2020-02-01</span></i> <!----> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      JavaScript
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h1 id="javascript中各种源码实现（前端面试笔试必备）"><a href="#javascript中各种源码实现（前端面试笔试必备）" class="header-anchor">#</a> JavaScript中各种源码实现（前端面试笔试必备）</h1> <blockquote><p>能够手撕各种JavaScript原生函数，可以说是进大厂必备！同时对JavaScript源码的学习和实现也能帮助我快速扎实地提升自己的前端编程能力。虽然我的实力还远达不到大厂层次 争取三年工作经验进大厂   来自掘金</p></blockquote> <h2 id="实现一个new操作符"><a href="#实现一个new操作符" class="header-anchor">#</a> 实现一个new操作符</h2> <p>我们首先知道new做了什么：</p> <ol><li>创建一个空的简单JavaScript对象（即{}）；</li> <li>链接该对象（即设置该对象的构造函数）到另一个对象 ；</li> <li>将步骤（1）新创建的对象作为this的上下文 ；</li> <li>如果该函数没有返回对象，则返回this。</li></ol> <div class="language- line-numbers-mode"><pre class="language-text"><code>function create(Con, ...args){
  // 创建一个空的对象
  this.obj = {};
  // 将空对象指向构造函数的原型链
  Object.setPrototypeOf(this.obj, Con.prototype);
  // obj绑定到构造函数上，便可以访问构造函数中的属性，即this.obj.Con(args)
  let result = Con.apply(this.obj, args);
  // 如果返回的result是一个对象则返回
  // new方法失效，否则返回obj
  return result instanceof Object ? result : this.obj;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="实现一个array-isarray"><a href="#实现一个array-isarray" class="header-anchor">#</a> 实现一个Array.isArray</h2> <p>利用Object.prototype.toString</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>Array.myIsArray = function(o) { 
  return Object.prototype.toString.call(Object(o)) === '[object Array]'; 
};
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="实现一个object-create-方法"><a href="#实现一个object-create-方法" class="header-anchor">#</a> 实现一个Object.create()方法</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>function create =  function (o) {
    var F = function () {};
    F.prototype = o;
    return new F();
};
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="实现一个eventhub-发布订阅"><a href="#实现一个eventhub-发布订阅" class="header-anchor">#</a> 实现一个EventHub 发布订阅</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>class Event {
  constructor () {
    // 储存事件的数据结构
    // 为查找迅速， 使用对象（字典）缓存
    this._cache = {}
  }

  // 绑定
  on(type, callback) {
    // 为了按类查找方便和节省空间
    // 将同一类型事件放到一个数组中
    // 这里的数组是队列， 遵循先进先出
    // 即新绑定的事件先触发
    let fns = (this._cache[type] = this._cache[type] || [])
    if(fns.indexOf(callback) === -1) {
      fns.push(callback)
    }
    return this
    }

  // 解绑
  off (type, callback) {
    let fns = this._cache[type]
    if(Array.isArray(fns)) {
      if(callback) {
        let index = fns.indexOf(callback)
        if(index !== -1) {
          fns.splice(index, 1)
        }
      } else {
        // 全部清空
        fns.length = 0
      }
    }
    return this
  }
  // 触发emit
  trigger(type, data) {
    let fns = this._cache[type]
    if(Array.isArray(fns)) {
      fns.forEach((fn) =&gt; {
        fn(data)
      })
    }
    return this
  }

  // 一次性绑定
  once(type, callback) {
    let wrapFun = () =&gt; {
      callback.call(this);
      this.off(type, callback);
    };
    this.on(wrapFun, callback);
    return this;
  }
}

let E = new Event()

E.on('click',function(){
  console.log('on')
})
E.trigger('click', '123')
console.log(e)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br></div></div><h2 id="实现一个array-prototype-reduce"><a href="#实现一个array-prototype-reduce" class="header-anchor">#</a> 实现一个Array.prototype.reduce</h2> <ul><li>Array.prototype.reduce语法</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>Array.prototype.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>实现方法如下</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>Array.prototype.myReduce = function(callback, initialValue) {
  let accumulator = initialValue ? initialValue : this[0];
  for (let i = initialValue ? 0 : 1; i &lt; this.length; i++) {
    let _this = this;
    accumulator = callback(accumulator, this[i], i, _this);
  }
  return accumulator;
};

// 使用
let arr = [1, 2, 3, 4];
let sum = arr.myReduce((acc, val) =&gt; {
  acc += val;
  return acc;
}, 5);

console.log(sum); // 15
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h2 id="实现一个call或apply"><a href="#实现一个call或apply" class="header-anchor">#</a> 实现一个call或apply</h2> <h3 id="call"><a href="#call" class="header-anchor">#</a> call</h3> <p>先来看一个call实例，看看call到底做了什么：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let foo = {
  value: 1
};
function bar() {
  console.log(this.value);
}
bar.call(foo); // 1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>从代码的执行结果，我们可以看到，call首先改变了this的指向，使函数的this指向了foo,然后使bar函数执行了。
总结一下：</p> <ol><li>call改变函数this指向</li> <li>调用函数</li></ol> <ul><li>代码如下：</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>Function.prototype.myCall = function(context) {
  context = context || window;
  //将函数挂载到对象的fn属性上
  context.fn = this;
  //处理传入的参数
  const args = [...arguments].slice(1);
  //通过对象的属性调用该方法
  const result = context.fn(...args);
  //删除该属性
  delete context.fn;
  return result
};
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>我们看一下上面的代码：</p> <ol><li>首先我们对参数context做了兼容处理，不传值，context默认值为window；</li> <li>然后我们将函数挂载到context上面,context.fn = this；</li> <li>处理参数，将传入myCall的参数截取，去除第一位，然后转为数组；</li> <li>调用context.fn，此时fn的this指向context；</li> <li>删除对象上的属性 delete context.fn；</li> <li>将结果返回。</li></ol> <h3 id="实现一下apply，唯一不同的是参数的处理-代码如下："><a href="#实现一下apply，唯一不同的是参数的处理-代码如下：" class="header-anchor">#</a> 实现一下apply，唯一不同的是参数的处理,代码如下：</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>Function.prototype.myApply = function(context) {
  context = context || window
  context.fn = this
  let result
  // myApply的参数形式为(obj,[arg1,arg2,arg3]);
  // 所以myApply的第二个参数为[arg1,arg2,arg3]
  // 这里我们用扩展运算符来处理一下参数的传入方式
  if (arguments[1]) {
    result = context.fn(…arguments[1])
  } else {
    result = context.fn()
  }
  delete context.fn;
  return result
};
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="实现一个function-prototype-bind"><a href="#实现一个function-prototype-bind" class="header-anchor">#</a> 实现一个Function.prototype.bind</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>function Person(){
  this.name=&quot;zs&quot;;
  this.age=18;
  this.gender=&quot;男&quot;
}
let obj={
  hobby:&quot;看书&quot;
}
//  将构造函数的this绑定为obj
let changePerson = Person.bind(obj);
//  直接调用构造函数,函数会操作obj对象,给其添加三个属性;
changePerson();
//  1、输出obj
console.log(obj);
//  用改变了this指向的构造函数,new一个实例出来
let p = new changePerson();
// 2、输出obj
console.log(p);
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>仔细观察上面的代码，再看输出结果。</p> <p>我们对Person类使用了bind将其this指向obj，得到了changeperson函数，此处如果我们直接调用changeperson会改变obj，若用new调用changeperson会得到实例 p,并且其__proto__指向Person,我们发现bind失效了。</p> <p>我们得到结论：<strong>用bind改变了this指向的函数，如果用new操作符来调用，bind将会失效。</strong></p> <p>这个对象就是这个构造函数的实例，那么只要在函数内部执行 this instanceof 构造函数 来判断其结果是否为true，就能判断函数是否是通过new操作符来调用了，若结果为true则是用new操作符调用的，代码修正如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>// bind实现
Function.prototype.mybind = function(){
  // 1、保存函数
  let _this = this;
  // 2、保存目标对象
  let context = arguments[0]||window;
  // 3、保存目标对象之外的参数,将其转化为数组;
  let rest = Array.prototype.slice.call(arguments,1);
  // 4、返回一个待执行的函数
  return function F(){
    // 5、将二次传递的参数转化为数组;
    let rest2 = Array.prototype.slice.call(arguments)
    if(this instanceof F){
      // 6、若是用new操作符调用,则直接用new 调用原函数,并用扩展运算符传递参数
      return new _this(...rest2)
    }else{
      //7、用apply调用第一步保存的函数，并绑定this，传递合并的参数数组，即context._this(rest.concat(rest2))
      _this.apply(context,rest.concat(rest2));
    }
  }
};
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h2 id="实现一个js函数柯里化"><a href="#实现一个js函数柯里化" class="header-anchor">#</a> 实现一个JS函数柯里化</h2> <p>Currying的概念其实并不复杂，用通俗易懂的话说：只传递给函数一部分参数来调用它，让它返回一个函数去处理剩下的参数。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>function progressCurrying(fn, args) {

    let _this = this
    let len = fn.length;
    let args = args || [];

    return function() {
        let _args = Array.prototype.slice.call(arguments);
        Array.prototype.push.apply(args, _args);

        // 如果参数个数小于最初的fn.length，则递归调用，继续收集参数
        if (_args.length &lt; len) {
            return progressCurrying.call(_this, fn, _args);
        }

        // 参数收集完毕，则执行fn
        return fn.apply(this, _args);
    }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h2 id="手写防抖-debouncing-和节流-throttling"><a href="#手写防抖-debouncing-和节流-throttling" class="header-anchor">#</a> 手写防抖(Debouncing)和节流(Throttling)</h2> <h3 id="节流"><a href="#节流" class="header-anchor">#</a> 节流</h3> <p>防抖函数 onscroll 结束时触发一次，延迟执行</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>function debounce(func, wait) {
  let timeout;
  return function() {
    let context = this; // 指向全局
    let args = arguments;
    if (timeout) {
      clearTimeout(timeout);
    }
    timeout = setTimeout(() =&gt; {
      func.apply(context, args); // context.func(args)
    }, wait);
  };
}
// 使用
window.onscroll = debounce(function() {
  console.log('debounce');
}, 1000);
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h3 id="节流-2"><a href="#节流-2" class="header-anchor">#</a> 节流</h3> <p>节流函数 onscroll 时，每隔一段时间触发一次，像水滴一样</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>function throttle(fn, delay) {
  let prevTime = Date.now();
  return function() {
    let curTime = Date.now();
    if (curTime - prevTime &gt; delay) {
      fn.apply(this, arguments);
      prevTime = curTime;
    }
  };
}
// 使用
var throtteScroll = throttle(function() {
  console.log('throtte');
}, 1000);
window.onscroll = throtteScroll;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="手写一个js深拷贝"><a href="#手写一个js深拷贝" class="header-anchor">#</a> 手写一个JS深拷贝</h2> <h3 id="丐版"><a href="#丐版" class="header-anchor">#</a> 丐版</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>JSON.parse(JSON.stringfy));
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>非常简单，但缺陷也很明显，比如拷贝其他引用类型、拷贝函数、循环引用等情况。</p> <h3 id="基础版"><a href="#基础版" class="header-anchor">#</a> 基础版</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>function clone(target){
  if(typeof target === 'object'){
    let cloneTarget = {};
    for(const key in target){
      cloneTarget[key] = clone(target[key])
    }
    return cloneTarget;
  } else {
    return target
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>写到这里已经可以帮助你应付一些面试官考察你的递归解决问题的能力。但是显然，这个深拷贝函数还是有一些问题。</p> <p>一个比较完整的深拷贝函数，需要同时考虑对象和数组，考虑循环引用：</p> <h3 id="旗舰版"><a href="#旗舰版" class="header-anchor">#</a> 旗舰版</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>function clone(target, map = new WeakMap()) {
  if(typeof target === 'object'){
    let cloneTarget = Array.isArray(target) ? [] : {};
    if(map.get(target)) {
      return target;
    }
    map.set(target, cloneTarget);
    for(const key in target) {
      cloneTarget[key] = clone(target[key], map)
    }
    return cloneTarget;
  } else {
    return target;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="实现一个instanceof"><a href="#实现一个instanceof" class="header-anchor">#</a> 实现一个instanceOf</h2> <p>原理： L 的 proto 是不是等于 R.prototype，不等于再找 L.<strong>proto</strong>.<strong>proto</strong> 直到 proto 为 null</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>// L 表示左表达式，R 表示右表达式
function instance_of(L, R) {
    var O = R.prototype;
  L = L.__proto__;
  while (true) {
        if (L === null){
            return false;
        }
        // 这里重点：当 O 严格等于 L 时，返回 true
        if (O === L) {
            return true;
        }
        L = L.__proto__;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="实现原型链继承"><a href="#实现原型链继承" class="header-anchor">#</a> 实现原型链继承</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>function myExtend(C, P) {
    var F = function(){};
    F.prototype = P.prototype;
    C.prototype = new F();
    C.prototype.constructor = C;
    C.super = P.prototype;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="实现一个事件代理"><a href="#实现一个事件代理" class="header-anchor">#</a> 实现一个事件代理</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;ul id=&quot;color-list&quot;&gt;
    &lt;li&gt;red&lt;/li&gt;
    &lt;li&gt;yellow&lt;/li&gt;
    &lt;li&gt;blue&lt;/li&gt;
    &lt;li&gt;green&lt;/li&gt;
    &lt;li&gt;black&lt;/li&gt;
    &lt;li&gt;white&lt;/li&gt;
&lt;/ul&gt;
&lt;script&gt;
    (function () {
      var color_list = document.getElementById('color-list');
      color_list.addEventListener('click', showColor, true);
      function showColor(e) {
        var x = e.target;
        if (x.nodeName.toLowerCase() === 'li') {
          alert(x.innerHTML);
        }
      }
    })();
&lt;/script&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="实现vue双向绑定"><a href="#实现vue双向绑定" class="header-anchor">#</a> 实现vue双向绑定</h2> <h3 id="vue-2-x的object-defineproperty版本"><a href="#vue-2-x的object-defineproperty版本" class="header-anchor">#</a> Vue 2.x的Object.defineProperty版本</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>// 数据
const data = {
  text: 'default'
};
const input = document.getElementById('input');
const span = document.getElementById('span');
// 数据劫持
Object.defineProperty(data, 'text', {
  // 数据变化 —&gt; 修改视图
  set(newVal) {
    input.value = newVal;
    span.innerHTML = newVal;
  }
});
// 视图更改 --&gt; 数据变化
input.addEventListener('keyup', function(e) {
  data.text = e.target.value;
});
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="vue-3-x的proxy-版本"><a href="#vue-3-x的proxy-版本" class="header-anchor">#</a> Vue 3.x的proxy 版本</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>// 数据
const data = {
  text: 'default'
};
const input = document.getElementById('input');
const span = document.getElementById('span');
// 数据劫持
const handler = {
  set(target, key, value) {
    target[key] = value;
    // 数据变化 —&gt; 修改视图
    input.value = value;
    span.innerHTML = value;
    return value;
  }
};
const proxy = new Proxy(data, handler);

// 视图更改 --&gt; 数据变化
input.addEventListener('keyup', function(e) {
  proxy.text = e.target.value;
});
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">最后更新时间: </span> <span class="time">4/4/2020, 8:45:17 PM</span></div></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-4698c43e><!----></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div></div></div>
    <script src="/blog/assets/js/app.c6f0b635.js" defer></script><script src="/blog/assets/js/3.56d0db2f.js" defer></script><script src="/blog/assets/js/1.dc177a2a.js" defer></script><script src="/blog/assets/js/21.5e84bd81.js" defer></script>
  </body>
</html>
